/*******************************************************************************
 * Copyright 2020 huanggefan.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

const tools = require("../js/tools");

window.Action_course_Create = function (major, name, description) {
    let request = tools.DeepCopy(window.API_Request_Course_CreateCourse);
    request.Name = name;
    request.Description = description;
    request.ManagerUUID = window.AuthTokenInfo.uuid;
    request.DisciplineUUID = major;
    request.AuthToken = window.AuthToken;

    let requestLoad = JSON.stringify(request);

    return tools.XHR(
        window.API_METHOD_POST,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        window.API_URL_Course_CreateCourse,
        requestLoad
    );
};

window.Action_course_Delete = function (uuid) {
    let request = tools.DeepCopy(window.API_Request_Course_DeleteCourse);
    request.WillDeleteUUID = uuid;
    request.AuthToken = window.AuthToken;

    let requestLoad = JSON.stringify(request);

    return tools.XHR(
        window.API_METHOD_POST,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        window.API_URL_Course_DeleteCourse,
        requestLoad
    );
};

window.Action_course_Get = function (uuid) {
    let request = tools.DeepCopy(window.API_Request_Course_GetCourse);
    request.CourseUUID = uuid;
    request.AuthToken = window.AuthToken;

    return tools.XHR(
        window.API_METHOD_GET,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        tools.StrFormat(window.API_URL_Course_GetCourse, [
            request.CourseUUID,
            request.AuthToken
        ]),
    );
};

window.Action_course_ListCollege = function (collegeUUID) {
    let request = tools.DeepCopy(window.API_Request_Course_ListCollegeCourse);
    request.CollegeUUID = collegeUUID;
    request.AuthToken = window.AuthToken;

    return tools.XHR(
        window.API_METHOD_GET,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        tools.StrFormat(window.API_URL_Course_ListCollegeCourse, [
            request.CollegeUUID,
            request.AuthToken
        ]),
    );
};

window.Action_course_ListMajor = function (majorUUID) {
    let request = tools.DeepCopy(window.API_Request_Course_ListDisciplineCourse);
    request.DisciplineUUID = majorUUID;
    request.AuthToken = window.AuthToken;

    return tools.XHR(
        window.API_METHOD_GET,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        tools.StrFormat(window.API_URL_Course_ListDisciplineCourse, [
            request.DisciplineUUID,
            request.AuthToken
        ]),
    );
};

window.Action_course_SetManager = function (courseUUID, userUUID) {
    let request = tools.DeepCopy(window.API_Request_Course_SetCourseManager);
    request.WillSetCourseUUID = courseUUID;
    request.WillSetManagerUUID = userUUID;
    request.AuthToken = window.AuthToken;

    let requestLoad = JSON.stringify(request);

    return tools.XHR(
        window.API_METHOD_POST,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        window.API_URL_Course_SetCourseManager,
        requestLoad
    );
};

window.Action_course_UnSetManager = function (courseUUID, userUUID) {
    let request = tools.DeepCopy(window.API_Request_Course_UnSetCourseManager);
    request.WillUnSetCourseUUID = courseUUID;
    request.WillUnSetManagerUUID = userUUID;
    request.AuthToken = window.AuthToken;

    let requestLoad = JSON.stringify(request);

    return tools.XHR(
        window.API_METHOD_POST,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        window.API_URL_Course_UnSetCourseManager,
        requestLoad
    );
};

window.Action_course_Update = function (uuid, name, description) {
    let request = tools.DeepCopy(window.API_Request_Course_UpdateCourse);
    request.NewName = name;
    request.NewDescription = description;
    request.WillUpdateUUID = uuid;
    request.AuthToken = window.AuthToken;

    let requestLoad = JSON.stringify(request);

    return tools.XHR(
        window.API_METHOD_POST,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        window.API_URL_Course_UpdateCourse,
        requestLoad
    );
};
